#ifndef MSGPUBSUB_H
#define MSGPUBSUB_H

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include "ComLog.h"
#ifdef __cplusplus
extern "C"
{
#endif

#define MSG_CENTER_LOG(fmt, ...)  COMLOG_DEBUG_LOG(fmt, ##__VA_ARGS__)

#define TIME_DELAY_MS (10*1000)

#define ANY_ID (0xFFFFFFFF)

typedef  void(*HandleEvent)(void *data, int srcId);

/* 订阅事件节点 */
typedef struct EventSubscriberNode
{
    char  event[32];
    HandleEvent func;
	int needId;
    struct EventSubscriberNode *next;
}EventSubscriberNode_t;


/* 发布事件节点 */ 
typedef struct EventPublishNode
{
    char  event[32]; 
    void  *data; //内部会拷贝一份，并自动释放
	int   srcId;
    struct EventPublishNode *next;
}EventPublishNode_t;

typedef struct MsgCenterHandle
{
	pthread_mutex_t       mutexSubscriber;
    pthread_mutex_t       mutexPublish;
	EventSubscriberNode_t *eventSubscriberHead;
	EventPublishNode_t    *eventPublishHead;
	volatile int                   exit_flg;
}MsgCenterHandle_t;


MsgCenterHandle_t *MsgCenterInit();
void MsgCenterDeinit(MsgCenterHandle_t **msg_handle);
int SubscriberEvent(MsgCenterHandle_t *handle, char * event, HandleEvent func, int needId);
int ReleaseSubscriberEvent(MsgCenterHandle_t *handle, char * event, HandleEvent func);
int PublishEvent(MsgCenterHandle_t *handle, char * event, void *data, int datalen, int srcId);

#ifdef __cplusplus
}
#endif

#endif